package com.company;
/**
 * @Description
 * @Auther zxh
 * @Date 2023/3/16:26 PM
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @Description
 * @Auther zxh
 * @Date 2023/3/16:26 PM
 */
public class CodeStatistics {
    static long classCount = 0;//java类数量
    //static long totalCount = 0;//总代码量
    static long blankCount = 0;//空行数
    static long sourceCount = 0;//代码行数
    static long commentCount = 0;//注释行数
    static long chartCount = 0;//中文字数
    static ArrayList<Integer> chartCountList = new ArrayList<>();//中文字数
    static List arrayList = new ArrayList<>();



    public static void main(String[] args) throws Exception {
        arrayList.add("MktServiceImpl.java");
        File f = new File("");
        String type = ".java";
        treeFile(f,type);
        System.out.println("代码行数："+sourceCount);
        System.out.println("注释行数："+commentCount);
        System.out.println("空行数："+blankCount);
        System.out.println("totalCount:"+(sourceCount + commentCount + blankCount));
        System.out.println("中文字数："+chartCountList.stream().mapToInt(Integer::intValue).sum());

    }

    public static void treeFile(File f, String type) throws Exception {
        long chartCount = 0;
        File[] listFiles = f.listFiles();
        for (int i = 0; i < listFiles.length; i++) {
            File file = listFiles[i];
            if (!file.isDirectory()) {
                if (file.getName().endsWith(type)) {
                    /*if (!arrayList.contains(file.getName())) {
                        continue;
                    }*/
                    classCount++;
                    BufferedReader br = null;
                    boolean comment = false;
                    br = new BufferedReader(new FileReader(file));
                    String line = "";
                    while ((line = br.readLine()) != null) {
                        getChart(line,chartCountList);
                        line = line.trim();
                        if (line.matches("^[//s&&[^//n]]*$")) {//这一行匹配以空格开头，但不是以回车符开头，但是以回车符结尾
                            blankCount++;
                        } else if (line.startsWith("/*") && !line.endsWith("*/")) {//匹配/**.....**/括住的多行注释
                            commentCount++;
                            comment = true;
                        } else if (true == comment) {
                            commentCount++;
                            if (line.endsWith("*/")) {
                                comment = false;
                            }
                            //匹配以//开头的单行注释，及有/**.....*/括住的当行注释
                        } else if (line.startsWith("//") || (line.startsWith("/**") && line.endsWith("*/"))) {
                            commentCount++;
                        } else {//其他的就是代码行
                            sourceCount++;
                        }
                    }
                    if (br != null) {
                        br.close();
                    }
                }
            } else {
                treeFile(listFiles[i], type);
            }

        }

    }

    public static void getChart(String line,List chartCount){
        String regEx = "[^\u4e00-\u9fa5]+";
        String[] split = line.split(regEx);
        for (int i = 0; i < split.length; i++) {
            chartCount.add(split[i].length());
        }
    }

}
